package com.gxa.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.pojo.Visitor;
import com.gxa.pojo.VisitorVO;
import com.gxa.service.VisitorService;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @description: TODO
* @author JZ
* @date 2022/9/3 10:05
* @version 1.0
*/
@Api(tags = "访客相关业务")
@RestController
@RequestMapping("visitor")
public class VisitorController {
        @Resource
        VisitorService service;

        @PostMapping("/insert")
        @ApiOperation(value="增加访客",httpMethod = "POST",response = boolean.class)
        @ApiResponse(code = 200,message = "添加成功")
        @ResponseBody
        boolean insertVisitor(@ApiParam(name = "visitor" ,value = "访客对象",required = true)@RequestBody Visitor visitor){
            return service.insertVisitor(visitor);
        }
        @ApiOperation(value="删除访客",httpMethod = "DELETE",response = boolean.class)
        @ApiResponse(code = 200,message = "删除成功")
        @ResponseBody
        @DeleteMapping("/delete")
        boolean deleteVisitor(@ApiParam(name = "id",value = "访客id",required = true) Integer id){
            return service.deleteVisitor(id);
        }
        @ApiOperation(value="显示访客",httpMethod = "POST",response = ResponseData.class)
        @ApiResponse(code = 200,message = "显示成功")
        @ResponseBody
        @PostMapping("/show")
        ResponseData selectVisitor(@ApiParam(name = "visitor",value = "访客对象",required=true)@RequestBody VisitorVO visitor){
            PageHelper.startPage(Integer.parseInt(visitor.getPage()),Integer.parseInt(visitor.getLimit()));
            List<Visitor> visitors = service.selectVisitor(visitor);
            PageInfo<Visitor> visitorPageInfo = new PageInfo<>(visitors);
            ResponseData responseData = ResponseData.successData(visitors);
            responseData.setCount((int) visitorPageInfo.getTotal());
            System.out.println(responseData);
            return responseData;
        }

        @ApiOperation(value="修改访客",httpMethod = "PUT",response = ResponseData.class)
        @ApiResponse(code = 200,message = "修改成功")
        @ResponseBody
        @PutMapping("/update")
        ResponseData updateVisitor(@ApiParam(name = "visitor",value = "访客对象",required = true) Visitor visitor){
            ResponseData data=new ResponseData();
            if (service.updateVisitor(visitor)){
                data.setCode(200);
                data.setMsg("修改成功");
            }
            return data;
        }

    }
